package com.teacher.service.impl;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.teacher.common.CommonResult;
import com.teacher.mapper.DepartmentMapper;
import com.teacher.pojo.entity.Department;
import com.teacher.pojo.vo.department.DepartmentAddVo;
import com.teacher.pojo.vo.department.DepartmentTreeVo;
import com.teacher.service.IDepartmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zfm
 * @since 2021-06-16
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    /**
     * @param departmentAddVo department's info
     * @return weather success
     */
    @Override
    public CommonResult<String> addDepartment(DepartmentAddVo departmentAddVo) {
        Department department = new Department();
        BeanUtils.copyProperties(departmentAddVo, department);
        // judge whether department's name repeat
        if (baseMapper.selectList(new QueryWrapper<Department>().lambda()
                .eq(Department::getParentId, departmentAddVo.getParentId())
                .eq(Department::getName, department.getName())).size() > 0) {
            return CommonResult.failed("部门名称重复");
        }
        if (baseMapper.insert(department) == 1) {
            return CommonResult.success("添加成功");
        }
        return CommonResult.failed("添加失败");
    }

    /**
     * get department by shape of tree
     *
     * @return department's info of shape of tree
     */
    @Override
    public CommonResult<List<DepartmentTreeVo>> getByTree() {
        List<Department> departmentList = baseMapper.selectList(new QueryWrapper<Department>().lambda()
                .orderByDesc(Department::getSort));
        List<DepartmentTreeVo> treeVoList = new ArrayList<>();
        List<DepartmentTreeVo> treeVoEndList = new ArrayList<>();


        for (Department department : departmentList) {
            DepartmentTreeVo departmentTreeVo = new DepartmentTreeVo();
            BeanUtils.copyProperties(department, departmentTreeVo);
            treeVoList.add(departmentTreeVo);
        }

        for (DepartmentTreeVo departmentTreeVo : treeVoList) {
            if (departmentTreeVo.getParentId() == 0 || "0".equals(departmentTreeVo.getParentId())) {
                treeVoEndList.add(departmentTreeVo);
                continue;
            }
            for (DepartmentTreeVo treeVo : treeVoList) {
                System.out.println(JSONUtil.toJsonStr(treeVo));
                if (treeVo.getId().equals(departmentTreeVo.getParentId())) {
                    if (treeVo.getChildList() == null) {
                        treeVo.setChildList(new ArrayList<>());
                    }
                    treeVo.getChildList().add(departmentTreeVo);
                }
            }
        }
        return CommonResult.success(treeVoEndList);
    }
}
